using System;
using System.Collections.Generic;
using System.Text;

namespace WxLib.Utility
{
    public class RequestBuilder
    {
        public RequestBuilder(){}
        public RequestBuilder(string host, IDictionary<string, string> requestParams)
        {
            Host = host;
            RequestParams = requestParams;
        }

        public string Host{get;set;}
        public IDictionary<string,string> RequestParams{get;set;}

        public string GetUrl()
        {
            if (string.IsNullOrEmpty(Host))
                throw new ArgumentException("host must be not null or empty!");

            StringBuilder urlBuilder = new StringBuilder(Host);

            if (Host.EndsWith("//"))
                urlBuilder.Remove(urlBuilder.Length - 1, 1);

            if (null != RequestParams && 0 != RequestParams.Count)
            {
                urlBuilder.Append("?");
                foreach (string prmKey in RequestParams.Keys)
                {
                    string prmValue = string.Empty;
                    RequestParams.TryGetValue(prmKey, out prmValue);
                    urlBuilder.AppendFormat("{0}={1}&", prmKey, prmValue);
                }
                urlBuilder.Remove(urlBuilder.Length - 1, 1);
            }

            return urlBuilder.ToString();
        }
    }
}